##########################################
# Replication Data for Proksch, Lowe, Wäckerle, Soroka. (2018). Multilingual Sentiment Analysis: A New Approach to Measuring Conflict in Legislative Speeches. Legislative Studies Quarterly, Forthcoming.
##########################################

#Part 4: Debate Sentiment and the Legislative Process in Germany
##########################
# Intro

rm(list = ls())
library(rstudioapi)
library(tidyverse)
library(ggplot2)
library(stargazer)
library(lubridate)
library(Zelig)
library(scales)

current_path <- getActiveDocumentContext()$path 
setwd(dirname(current_path ))

load("4_germany_datasets.RData")

##########################
# Plot By Party

plot_data=senti_germany_debates%>%filter(!group%in%c("external","fraktionslos")&!is.na(group))
plot_data$month=format(as.Date(plot_data$Date_of_thisspeech), "%Y-%m")
plot_data$yearonly=format(as.Date(plot_data$Date_of_thisspeech), "%Y")
plot_data$date_new=as.Date(paste0(plot_data$month,'-01'), format='%Y-%m-%d')
plot_data$GovOppo="Opposition"
plot_data$GovOppo[plot_data$group=="CDU/CSU"]="Government"
plot_data$GovOppo[plot_data$group=="SPD"&plot_data$Legislative_period%in%c(16,18)]="Government"
plot_data$GovOppo[plot_data$group=="FDP"&plot_data$Legislative_period%in%c(17)]="Government"
plot_data$GovOppo[plot_data$group=="Cabinet"]="Cabinet"


plot_data_year_group=aggregate(Sentiment~group+date_new,plot_data,FUN=mean)

plot_data_year_group$group=factor(plot_data_year_group$group)
plot_data_year_group$group=relevel(plot_data_year_group$group, "Cabinet")

plot_data_year_group$Legislative_period=16
plot_data_year_group$Legislative_period[plot_data_year_group$date_new>as.Date("2009-09-21")]=17
plot_data_year_group$Legislative_period[plot_data_year_group$date_new>as.Date("2013-09-22")]=18
table(plot_data_year_group$Legislative_period)
plot_data_year_group$Year=format(as.Date(plot_data_year_group$date_new), "%Y")
p=ggplot(plot_data_year_group, aes(y=Sentiment, x=date_new,colour=group,lty=group)) + 
  geom_smooth(size=2,alpha=0.2,method = 'loess')+
  scale_x_date(labels = date_format("%Y"),breaks=date_breaks('1 year'))+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size=16),
        axis.text.x = element_text(size=16),
        legend.text=element_text(size=16),
        axis.title=element_text(size=18),
        plot.title = element_text(size=20,hjust = 0.5))+
  theme(legend.position = "bottom",
        legend.title = element_blank()) +
  labs(y="Sentiment",x="")+
  scale_colour_manual(values=c("orange","#00A646","black","#FF0000","#ffed00","#DF0029"))+
  geom_vline(xintercept = as.numeric(as.Date("2009-09-21")), linetype="solid", col="grey") +
  geom_vline(xintercept = as.numeric(as.Date("2013-09-22")), linetype="solid", col="grey") +
  annotate("text", x=as.Date("2007-06-01"), y=1.4, label="CDU/CSU-SPD Coalition", size=6) +
  annotate("text", x=as.Date("2011-08-01"), y=1.4, label="CDU/CSU-FDP Coalition", size=6) +
  annotate("text", x=as.Date("2015-07-01"), y=1.4, label="CDU/CSU-SPD Coalition", size=6)


##########################
# Plot By Government-Opposition

plot_data=senti_germany_debates%>%filter(!group%in%c("external","fraktionslos")&!is.na(group))
plot_data$month=format(as.Date(plot_data$Date_of_thisspeech), "%Y")
plot_data$yearonly=format(as.Date(plot_data$Date_of_thisspeech), "%Y")
plot_data$date_new=as.Date(paste0(plot_data$month,'-01-01'), format='%Y-%m-%d')
plot_data$GovOppo="Opposition"
plot_data$GovOppo[plot_data$group=="CDU/CSU"]="Government"
plot_data$GovOppo[plot_data$group=="SPD"&plot_data$Legislative_period%in%c(16,18)]="Government"
plot_data$GovOppo[plot_data$group=="FDP"&plot_data$Legislative_period%in%c(17)]="Government"
plot_data$GovOppo[plot_data$group=="Cabinet"]="Cabinet"

plot_data_month_govoppo=aggregate(Sentiment~GovOppo+date_new,plot_data,FUN=mean)
str(plot_data_month_govoppo)

plot_data_month_govoppo$GovOppo=factor(plot_data_month_govoppo$GovOppo)
plot_data_month_govoppo$GovOppo=relevel(plot_data_month_govoppo$GovOppo, "Government")
plot_data_month_govoppo$GovOppo=relevel(plot_data_month_govoppo$GovOppo, "Opposition")

plot_data_month_govoppo$Legislative_period=16
plot_data_month_govoppo$Legislative_period[plot_data_month_govoppo$date_new>as.Date("2009-09-21")]=17
plot_data_month_govoppo$Legislative_period[plot_data_month_govoppo$date_new>as.Date("2013-09-22")]=18
table(plot_data_month_govoppo$Legislative_period)
plot_data_month_govoppo$Year=format(as.Date(plot_data_month_govoppo$date_new), "%Y")

p=ggplot(plot_data_month_govoppo, aes(y=Sentiment, x=date_new,colour=GovOppo,shape=GovOppo,lty=GovOppo)) + 
  geom_smooth(colour="grey55",lty="solid",size=1,alpha=0.1,method = 'loess')+
  geom_line(size=1)+
  geom_point(size=4)+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size=16),
        axis.text.x = element_text(size=16),
        legend.text=element_text(size=16),
        axis.title=element_text(size=18),
        plot.title = element_text(size=20,hjust = 0.5))+
  theme(legend.position = "bottom",
        legend.title = element_blank()) +
  labs(y="Sentiment",x="")+
  scale_x_date(breaks = sort(unique(plot_data_month_govoppo$date_new)), labels=sort(unique(plot_data_month_govoppo$Year))) +
  geom_vline(xintercept = as.numeric(as.Date("2009-09-21")), linetype="solid", col="grey") +
  geom_vline(xintercept = as.numeric(as.Date("2013-09-22")), linetype="solid", col="grey") +
  annotate("text", x=as.Date("2007-06-01"), y=1.4, label="CDU/CSU-SPD Coalition", size=6) +
  annotate("text", x=as.Date("2011-08-01"), y=1.4, label="CDU/CSU-FDP Coalition", size=6) +
  annotate("text", x=as.Date("2015-07-01"), y=1.4, label="CDU/CSU-SPD Coalition", size=6)

##########################
# Models

#Unanimity Models
mod_unanimity_no_control <- glm(unanimity_last_reading ~ Opposition_Sentiment_Overall,
                                metadata_df_speeches, family = binomial)
mod_unanimity_senticontrols <- glm(unanimity_last_reading ~ Opposition_Sentiment_Overall +Junior_Coalition_Partner_Sentiment_Overall+ Prime_Minister_Sentiment_Overall ,
                                   metadata_df_speeches, family = binomial)
mod_unanimity_complete <- glm(unanimity_last_reading ~  Opposition_Sentiment_Overall + Junior_Coalition_Partner_Sentiment_Overall+ Prime_Minister_Sentiment_Overall + Initiator2 +
                                main_policy_area_foreign_relations + main_policy_area_domestic +
                                main_policy_area_economy + main_policy_area_europe+Legislative_period,
                              metadata_df_speeches, family = binomial)

stargazer(mod_unanimity_no_control,mod_unanimity_senticontrols,mod_unanimity_complete,type="text")
stargazer(mod_unanimity_no_control,mod_unanimity_senticontrols,mod_unanimity_complete)

#Conflict Models, which are not included in the paper
mod_unanimity_conflict_no_control <- glm(unanimity_last_reading ~ Gov_Oppo_Conflict, 
                                         metadata_df_speeches, family = binomial)
mod_unanimity_conflict_senticontrols <- glm(unanimity_last_reading ~ Gov_Oppo_Conflict + Junior_Coalition_Partner_Sentiment_Overall+ Prime_Minister_Sentiment_Overall, 
                                            metadata_df_speeches, family = binomial)
mod_unanimity_conflict_complete <- glm(unanimity_last_reading ~ Gov_Oppo_Conflict + Junior_Coalition_Partner_Sentiment_Overall+ Prime_Minister_Sentiment_Overall+Initiator2 +main_policy_area_foreign_relations + main_policy_area_domestic + main_policy_area_economy + main_policy_area_europe+Legislative_period, 
                                       metadata_df_speeches, family = binomial)
stargazer(mod_unanimity_conflict_no_control,mod_unanimity_conflict_senticontrols,mod_unanimity_conflict_complete,type="text")

set.seed(1524)
#Run Zelig model to estimate predicted probabilities
mymod <- zelig(unanimity_last_reading ~ Junior_Coalition_Partner_Sentiment_Overall +
                 Opposition_Sentiment_Overall + Prime_Minister_Sentiment_Overall + as.factor(Initiator2) +
                 main_policy_area_foreign_relations + main_policy_area_domestic +
                 main_policy_area_economy + main_policy_area_europe+as.factor(Legislative_period),
               data=metadata_df_speeches, model = "logit")

#Simulate probabilities for 5% and 95% sentiment quantiles
quantile(metadata_df_speeches$Opposition_Sentiment_Overall,probs = c(.05,.95))
mymodx2 <- setx(mymod, Opposition_Sentiment_Overall = c(0.30,1.21),Initiator2="cabinet_bill",Legislative_period=17)
mymods2 <- sim(mymodx2)
mymods2

#Simulate for legislative periods
mymodx3<- setx(mymod,Legislative_period=c(16,17,18),Initiator2="cabinet_bill")
mymods3 <- sim(mymodx3)
mymods3

#Simulate for policy areas
mymodx4 <- setx(mymod,main_policy_area_foreign_relations=c(0,1),Legislative_period=16)
mymods4 <- sim(mymodx4)
mymods4
